package common.job
{
	import common.data.monitor.MonitorService;
	import common.util.DataUtil;
	import common.util.SystemUtil;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class RefreshAlarmJob
	{
		private static var job:RefreshAlarmJob;
		private var timer:Timer;
		private var loadingData:Boolean = true;
		private var monitorService:MonitorService;
		private var _searchKey:String = 'ALL';//查询常量
		private var isCompulsoryRefrush:Boolean = false;//是否要强制刷新 强制刷新的时候  不考虑loadingData
		private var _alarmNumMap:Array=[];// 报警编号|报警名称 — 报警数量 Map
		private var _alarmMap:Array=[];// 报警编号 — 报警明细 Map
		private var _alarmTypeNum:Number = 0;//记录当前有多少种类报警
		public var statBackFunction:Function;
		
		public function get alarmNumMap():Array
		{
			return this._alarmNumMap;
		}
		public function get alarmMap():Array
		{
			return this._alarmMap;
		}
		public function get alarmTypeNum():Number
		{
			return this._alarmTypeNum;
		}
		public function set searchKey(value:String):void
		{
			this._searchKey = value;
		}
		public static function getInstance():RefreshAlarmJob{
			if(!job){
				job = new RefreshAlarmJob();
			}
			return job;
		}
		
		public function RefreshAlarmJob(){
			monitorService = new MonitorService();
			this.timer = new Timer(10*1000);
			this.timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		//第一次加载
		public function fristLoad():void{
			compulsoryRefrush();
			timer.start();
		}
		
		//强制把报警刷一次
		public function compulsoryRefrush():void{
			isCompulsoryRefrush = true;
			onTimer(null);
		}
		
		/**
		 * 定时
		 */
		private function onTimer(e:TimerEvent):void{
			if(isCompulsoryRefrush){
				monitorService.getAlarmStatCount(_searchKey,getAlarmStatCountCallBack);
				isCompulsoryRefrush = false;
			}else{
				if(loadingData){
					monitorService.getAlarmStatCount(_searchKey,getAlarmStatCountCallBack);
					loadingData = false;
				}
			}
		}
		
		private function getAlarmStatCountCallBack(obj:Object):void{
			loadingData = true;
			if(!obj){
				return;
			}
			var i:Number = 0;
			_alarmNumMap=[];
			_alarmMap=[];
			_alarmTypeNum=0;
			
			
			for (var key:String in obj){
				var num:Number=0;
				if(obj[key] is Number){
					num = obj[key];
				}
				if(obj[key] is ArrayCollection){
					num = ArrayCollection(obj[key]).length ;
					_alarmMap[key] = obj[key];
				}
				_alarmNumMap[key]=num;//每次重新初始化
				_alarmTypeNum++;
				i += num;
			}
			
			if(statBackFunction){
				statBackFunction.apply(null,[i]);
			}
		}
		
		//处理报警后的处理
		public function dealAlarmHandle(obj:Object):void{
			//处理了报警  本地刷新报警数量
//			var alarm:VehicleAlarmInfoNew = obj as VehicleAlarmInfoNew;
//			var key:String = alarm.alarmType.alarmCode.concat("|").concat(alarm.alarmType.alarmName);
//			var num:Number = RefreshAlarmJob.getInstance().alarmNumMap[key] as Number;
//			if(num){
//				if(num>1){
//					//刷新数字缓存
//					_alarmNumMap[key] = num -1;
//					//刷新模型缓存
//					var arrayCollection:ArrayCollection = _alarmMap[alarm.alarmType.alarmCode] as ArrayCollection;
//					if(arrayCollection && arrayCollection.contains(alarm)){
//						var index:Number = arrayCollection.getItemIndex(alarm);
//						if(index != -1){
//							arrayCollection.removeItemAt(index);
//						}
//					}
//				}else{
//					_alarmNumMap[key] = null;
//					_alarmMap[alarm.alarmType.alarmCode] = null;
//					_alarmTypeNum -= 1;
//				}
//			}
//			
//			var i:Number = 0;
//			for each(var tempNum:Number in _alarmNumMap)
//			{
//				i+=tempNum;
//			}
//			
//			if(statBackFunction){
//				statBackFunction.apply(null,[i]);
//			}
		}
		
		public function startJob():void{
			this.timer.start();
		}
		
		
		public function stopJob():void{
			this.timer.stop();
		}
	}
}